package com.agency8x.model;

import com.agency8x.model.base.BaseRechargeModel;
import com.agency8x.util.DateTimeUtil;
import com.agency8x.util.RegexUtil;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;

/**
 * Recharge model. 数据库字段名建议使用驼峰命名规则，便于与 java 代码保持一致，如字段名： userId
 */
@SuppressWarnings("serial")
public class RechargeModel extends BaseRechargeModel<RechargeModel> {

    public static final RechargeModel me = new RechargeModel().dao();

    /**
     * 所有 sql 与业务逻辑写在 Service 中，在此放在 Model 中仅为示例
     */
    public Page<RechargeModel> paginate(int pageNumber, int pageSize, String search_word, String date1, String date2,Integer status) {
        String sql = "SELECT * ";
        String sqlFrom = " FROM t_finance_recharge where 1=1 ";

        if(status!=null)
        	sqlFrom = sqlFrom + " and status=" + status;
        /*----------------------搜索--------------------------*/
        if (StrKit.notBlank(date1) && DateTimeUtil.validateDate(date1)) {
        	sqlFrom += " and create_time >='" + date1 + " 0:0:0'";
        }
        if (StrKit.notBlank(date2) && DateTimeUtil.validateDate(date2)) {
        	sqlFrom += " and create_time <='" + date2 + " 23:59:59'";
        }

        if (search_word != null && !"".equals(search_word.trim()) && !"null".equals(search_word)) {
            if (RegexUtil.isNumeric(search_word)) {
                sqlFrom = sqlFrom + " and user_id =" + search_word;
            }
        }
        sqlFrom = sqlFrom + " order by create_time desc";
        return paginate(pageNumber, pageSize, sql, sqlFrom);
    }

    /**
     * 代理管理中的用户充值记录
     *
     * @param pageNumber
     * @param pageSize
     * @param user_id
     * @param search_word
     * @param beginDate
     * @param endDate
     * @return
     */
    public Page<RechargeModel> pagAgency(int pageNumber, int pageSize, String user_id, String search_word, String date1, String date2) {
        String sql = "SELECT * ";
        String sqlFrom = " FROM t_finance_recharge where 1=1 ";

        if (user_id != null && !"".equals(user_id.trim()) && !"null".equals(user_id)) {
            sqlFrom = sqlFrom + " and user_id =" + user_id;
        }

        /*----------------------搜索--------------------------*/
        if (StrKit.notBlank(date1) && DateTimeUtil.validateDate(date1)) {
        	sqlFrom += " and create_time >='" + date1 + " 0:0:0'";
        }
        if (StrKit.notBlank(date2) && DateTimeUtil.validateDate(date2)) {
        	sqlFrom += " and create_time <='" + date2 + " 23:59:59'";
        }

        if (search_word != null && !"".equals(search_word.trim()) && !"null".equals(search_word)) {
            if (RegexUtil.isNumeric(search_word)) {
                sqlFrom = sqlFrom + " and user_id =" + search_word;
            }
        }
        sqlFrom = sqlFrom + " order by create_time desc";
        return paginate(pageNumber, pageSize, sql, sqlFrom);
    }

    public boolean postting(String search_word, String beginDate, String endDate) {
        if (!"null".equals(beginDate) && !"null".equals(endDate) && !"null".equals(search_word)) {
            return true;
        }
        return false;
    }

    /**
     * 获取顶级代理所有下线的充值明细
     *
     * @param pageNumber
     * @param pageSize
     * @param top_agent_id
     * @param word
     * @param date1
     * @param date2
     * @return
     */
    public Page<RechargeModel> getPaginateByTopAgentId(int pageNumber, int pageSize, Integer top_agent_id, Integer status, String word, String date1, String date2) {
        return getPaginateByUser(pageNumber, pageSize, "top_agent_id=" + top_agent_id, status, word, date1, date2);
    }

    public Page<RechargeModel> getPaginateByUserId(int pageNumber, int pageSize, Integer user_id, Integer status, String word, String date1, String date2) {
        return getPaginateByUser(pageNumber, pageSize, "user_id=" + user_id, status, word, date1, date2);
    }

    public Page<RechargeModel> getPaginateByParent1Uid(int pageNumber, int pageSize, Integer parent1_uid, Integer status, String word, String date1, String date2) {
        return getPaginateByUser(pageNumber, pageSize, "parent1_uid=" + parent1_uid, status, word, date1, date2);
    }

    public Page<RechargeModel> getPaginateByParent2Uid(int pageNumber, int pageSize, Integer parent2_uid, Integer status, String word, String date1, String date2) {
        return getPaginateByUser(pageNumber, pageSize, "parent2_uid=" + parent2_uid, status, word, date1, date2);
    }

    private Page<RechargeModel> getPaginateByUser(int pageNumber, int pageSize, String userCondition, Integer status, String word, String date1, String date2) {
        String sql = "SELECT u.user_id, u.nickname, r.id, r.serial_no, r.out_trade_no, r.amount, r.pay_channel, r.remark, r.is_agent, r.create_time";
        String sqlFrom = "FROM dbo.t_finance_recharge r LEFT JOIN dbo.t_user u ON r.user_id=u.user_id";
        String sqlWhere = " WHERE u." + userCondition + " and r.status=1";
        String sqlOrder = " ORDER BY r.id desc";
        if (StrKit.notBlank(date1) && DateTimeUtil.validateDate(date1)) {
            sqlWhere += " and r.create_time >='" + date1 + " 0:0:0'";
        }
        if (StrKit.notBlank(date2) && DateTimeUtil.validateDate(date2)) {
            sqlWhere += " and r.create_time <='" + date2 + " 23:59:59'";
        }

        if (StrKit.notBlank(word)) {
        	if (RegexUtil.isNumeric(word)){
        		sqlWhere = sqlFrom + " and u.user_id like '%" + word + "%'";
        	}			else{
            sqlWhere += " and (u.user_id like '%" + word + "%' or u.nickname like '%" + word + "%')";
        	}
        	
        }
        sqlFrom += sqlWhere + sqlOrder;
        return paginate(pageNumber, pageSize, sql, sqlFrom);
    }


    public String getCreateTimeText() {
        return DateTimeUtil.formatSqlServerDateTime(getCreateTime());
    }

    public String getAuditTimeText() {
        return DateTimeUtil.formatSqlServerDateTime(getAuditTime());
    }

    public String getStatusText() {
        Short status = get("status");
        switch (status) {
            case 0:
                return "待审核";
            case 1:
                return "通过";
            case 2:
                return "驳回";
            default:
                return "-";
        }
    }
}
